#!/bin/bash

export BUILD_ID=dontKillMe

#进入源码目录
cd src
#抹掉记录
git checkout src-web/src/main/resources/application.yml
#替换监听端口
sed -i "s/8000/82/g" sys-web/src/main/resources/application.yml
sed -i "s/127.0.0.1/10.10.10.81/g" sys-web/src/main/resources/application.yml
sed -i "s/exam/jxtk/g" sys-web/src/main/resources/application.yml
sed -i "s/username: root/username: jxadmin/g" sys-web/src/main/resources/application.yml
sed -i "s/password: root/password: 123456/g" sys-web/src/main/resources/application.yml
#打包
mvn package

#杀死java进程
kill $(ps -aux | grep exam | grep -v grep | awk '{print $2}')
#循环，等着java进程结束
while true; do
  netstat -anp | grep 82 -w
  used=$(netstat -anp | grep LISTEN | grep 82 -w) 
 if [ -n "$used" ]; then
     sleep 5
else
  break
fi
done 
#删除旧的，cp新的
rm -rf /opt/jxtk-svc/*
cp -r sys-web/target/config/ sys-web/target/exam-4.1.0.jar /opt/jxtk-svc/
#进入到cp后的目录
cd /opt/jxtk-svc/
#进入到要发布的目录/opt/jxtk-svc下，启动java进程并放入后台运行
#nohup java -jar exam-4.1.0.jar &
java -jar exam-4.1.0.jar >> jxtk-svc.log 2>&1 &
#测试5次，并延迟5s
#定义变量i=0,当i<5,继续，i++
i=0
while [ $i -lt 5 ]; do
curl 'http://localhost:82/api/login/sysTime' -X 'POST'
#如果命令成功，跳出循环，不成功睡觉5s
if [ $? -eq 0 ]; then
   exit 0
else
   sleep 5
fi
i=$(echo $i+1 | bc)
done 
